﻿<?php

include("inc_configuration.php");
include("classes/cuser.php");

$tID=0;
$et=0;
$gt=0;
$pk=0;
$sp=0;
$anz=0;



$tQ = "SELECT t.id FROM $tbl_teams t, $tbl_lt lt WHERE lt.leagueId=$liga AND lt.teamId=t.id";
$tQe = mysql_query($tQ);

while ($row = mysql_fetch_object($tQe))
{
	$tID=$row->id;
	
	$team = array(
			  tID => $tID,
			  et => 0,
			  gt => 0,
			  sp => 0,
			  s => 0,
			  n => 0,
			  u => 0);
	
	$teams[] = $team;
	$anz++;
}

$mQ = "SELECT * FROM $tbl_matches WHERE leagueId=$liga AND free>=3 AND matchday<($matchday+1)";
$mQe = mysql_query($mQ);

while ($row = mysql_fetch_object($mQe))
{
	
	$hT = $row->homeT;
	$gT = $row->guestT;
	$count=0;
	for($count = 0; $count < $anz; $count++)
			{
				$tI=$teams[$count][tID];

				if (($hT==$tI) OR ($gT==$tI))
				{
					$aT=$teams[$count];
					$hG = $row->homeG;
					$gG = $row->guestG;
					$wi = $row->winner;
					
					$aT[sp]=$aT[sp]+1;
					if ($hT==$tI)
					{
						$aT[et]= $aT[et]+$hG;
						$aT[gt]= $aT[gt]+$gG;
						if ($wi==1)
						{
							$aT[s]=$aT[s]+1;
						}
						else
						{
							if ($wi==2)
							{
								$aT[n]=$aT[n]+1;
							}
							else
							{
								$aT[u]=$aT[u]+1;
							}
						}
					}
					else
					{
						if ($gT==$tI)
						{
							$aT[et] = $aT[et]+$gG;
							$aT[gt] = $aT[gt]+$hG;
							if ($wi==1)
							{
								$aT[n]=$aT[n]+1;
							}
							else
							{
								if ($wi==2)
								{
									$aT[s]=$aT[s]+1;
								}
								else
								{
									$aT[u]=$aT[u]+1;
								}
							}
						}
					}
					$teams[$count]=$aT;
				}
			}
}

$count=0;
for($count = 0; $count < $anz; $count++)
{
	$tA=$teams[$count];
	$userId=$jUser;
	$teamId="$tA[tID]";
	$et="$tA[et]";
	$gt="$tA[gt]";
	$td=$et-$gt;
	$s="$tA[s]";
	$u="$tA[u]";
	$n="$tA[n]";
	$pkt=($s*3)+($u);
	$sp="$tA[sp]";
	
	$tableQ="INSERT INTO 	$tbl_table (userId,teamId,sp,s,u,n,et,gt,td,pkt) 
							VALUES ($userId,$teamId,$sp,$s,$u,$n,$et,$gt,$td,$pkt)";
	$tableQe=mysql_query($tableQ);
	
	
}


$tsQ="	SELECT t.id, t.name, lt.sp,lt.s,lt.u,lt.n,lt.et,lt.gt,lt.td,lt.pkt 
		FROM $tbl_table lt, $tbl_teams t 
		WHERE
		lt.teamId=t.id AND
		lt.userId=$jUser
		ORDER BY pkt DESC,td DESC,et DESC";
$tsQe=mysql_query($tsQ);

echo "<table id='tborder' bgcolor='#DADADA'>";

echo "	<thead id='tabelle'><tr>
					<th>Platz</th>
					<th colspan='2'>Mannschaft</th>
					<th>Trainer</th>
					<th>Spiele</th>
					<th>S</th>
					<th>U</th>
					<th>N</th>
					<th>T</th>
					<th>GT</th>
					<th>TD</th>
					<th>Punkte</th>
				</tr>
		</thead>";
				

$p=1;

	while ($row = mysql_fetch_object($tsQe))
	{

		$tI = $row->id;

		$uQ="	SELECT username AS uGn, c.id AS gC, u.id AS uId FROM $tbl_combinations c, $tbl_users u
				WHERE
				(c.teamId=$tI) AND
				(c.active=1) AND
				(c.userId=u.id)";
				
		$uQe = mysql_query($uQ);

		$uQc = mySQL_num_rows($uQe);
		
		if ($uQc==0)
		{
			$cN="(-)";
		}
		else
		{
			$cNq = mysql_fetch_object($uQe);
			
			$cN=$cNq->uGn;
			
			$uId=$cNq->uId;
			
			// $cL="<a href='profil.php?id=";
			// $cL.="$uId'>";
			// $cL.="$cN</a>";
			
			$user=new Cuser($uId);
	
			
			
			$cN=$user->usernamelink;
		}
		
		$tN=$row->name;
		$et=$row->et;
		$gt=$row->gt;
		$td=$row->td;
		$s=$row->s;
		$u=$row->u;
		$n=$row->n;
		$pkt=$row->pkt;
		$sp=$row->sp;
		$tid=$row->id;
		
		if ($td>0)
		{
			$tdz=$td;
			$td="+$tdz";
		}
		
	//FARBEN	
		$color="";
		
		$q = "SELECT *
				FROM
					$tbl_rankings
				WHERE
					(leagueId=$liga) AND
					($p>=start) AND
					($p<=end)";
					
	
	
		$qe = mysql_query($q);
		
		$qec = mysql_num_rows($qe);
		
		if ($qec>0)
		{
			$qeo = mysql_fetch_object($qe);
			$ccode=$qeo->color;
			$color="bgcolor='$ccode'";
		}
		
		
		echo "	<tr $color  id='line'>
					<td>$p.</td>
					<td><a href='team_kader.php?team=$tid'><img src='img/logos/",$tI,".png' width='35' height='35' alt='' align='middle'></a></td>
					<td><a href='spiele_team.php?team=$tid&liga=$liga'>$tN</a></td>
					<td>$cN</td>
					<td>$sp</td>
					<td>$s</td>
					<td>$u</td>
					<td>$n</td>
					<td>$et</td>
					<td>$gt</td>
					<td>$td</td>
					<td>$pkt</td>
				</tr>";
		
		
	$p++;
	}
	
	echo "	<thead id='tabelle'><tr>
					<th colspan='12'></th>
				</tr>
		</thead>";
		
	echo "</table>";
	
	$l = "DELETE FROM $tbl_table WHERE userId=$jUser";
	$le = mysql_query($l);




?>